package org.yankun.exam4j.web.controller.dashboard;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.yankun.exam4j.core.paper.service.PaperService;
import org.yankun.exam4j.core.question.service.QuestionService;
import org.yankun.exam4j.entity.CandidateInput;
import org.yankun.exam4j.entity.CandidateSelect;
import org.yankun.exam4j.entity.Paper;
import org.yankun.exam4j.entity.PaperQuestion;
import org.yankun.exam4j.entity.Question;
import org.yankun.exam4j.entity.QuestionTypeConfig;
import org.yankun.exam4j.entity.User;
import org.yankun.exam4j.security.user.service.UserService;
import org.yankun.exam4j.session.SessionManager;
import org.yankun.exam4j.session.UserSessionData;


@Controller("QuestionManageController")
@RequestMapping("/dashboard/question")
public class QuestionManageController {
	
	@Resource
	private QuestionService questionService;
	
	@Resource
	private PaperService paperService;
	
	@Resource
	private UserService userService;
	
	@RequestMapping("index")
	public ModelAndView toIndex(HttpServletResponse response,HttpServletRequest request,String email,String password){
		return new ModelAndView("/dashboard/question/index");
	}
	
	@RequestMapping("toAddQuestion")
	public ModelAndView toAddQuestion(HttpServletResponse response,HttpServletRequest request){
		return new ModelAndView("/dashboard/question/add");
	}

	
	@RequestMapping(value="toAddQuestion/{paperId}")
	public ModelAndView toAddQuestionByPaperId(HttpServletResponse response,HttpServletRequest request,@PathVariable("paperId") Integer paperId){
		Map<String, Object> map=new HashMap<String, Object>();
		
		HttpSession session = request.getSession();
		UserSessionData usd = SessionManager.getSessionData(session);
		
		List<QuestionTypeConfig> typeList = questionService.getQuestionType();
		List<Paper> paperList = paperService.getPapersByUser(usd.getUserId());
		
		map.put("paperId", paperId);
		map.put("typeList", typeList);
		map.put("paperList", paperList);
		
		return new ModelAndView("/dashboard/question/add",map);
	}
	
	
	@RequestMapping("doAddQuestion")
	@ResponseBody
	public void doAddExam(HttpServletResponse response,HttpServletRequest request,Integer paperId,Question question,Integer questionType,String[] anwsers,String[] corrects){
		HttpSession session = request.getSession();
		UserSessionData usd = SessionManager.getSessionData(session);
		
		User user = userService.getUserById(usd.getUserId());
		
		QuestionTypeConfig questionTypeConfig = new QuestionTypeConfig();
		questionTypeConfig.setUid(questionType);
		
		question.setQuestionTypeConfig(questionTypeConfig);
		question.setCreateUserId(user.getUid());
		question.setCreateTime(new Date());
		question.setUpdateUserId(user.getUid());
		question.setUpdateTime(new Date());
		
		questionService.addQuestion(question);
		
		Set<CandidateSelect> selectList = null;
		Set<CandidateInput> inputList = null;
		
		if(null != anwsers){
			if(null != corrects&& questionType==1 ||questionType==2 ||questionType==3){
				selectList = new HashSet<CandidateSelect>();
				
				for(int i=0,n=anwsers.length; i<n; i++){
					CandidateSelect cs = new CandidateSelect();
					cs.setAnswer(anwsers[i]);
					
					for(int j=0,m=corrects.length; j<m; j++){
						if(i==j){
							cs.setCorrect(true);
						}else{
							cs.setCorrect(false);
						}
						
					}
					cs.setQuestion(question);
					questionService.addCandidateSelects(cs);
					
					
					selectList.add(cs);
				}
				question.setCandidateSelects(selectList);
			}else{
				inputList = new HashSet<CandidateInput>();
				
				for(int i=0,n=anwsers.length; i<n; i++){
					CandidateInput ci = new CandidateInput();
					ci.setAnswer(anwsers[i]);
					ci.setQuestion(question);
					questionService.addCandidateInput(ci);
					inputList.add(ci);
				}
				question.setCandidateInputs(inputList);
			}
		}
		
		PaperQuestion pq =new PaperQuestion();
		
		pq.setPaper(paperService.getPaperById(paperId));
		pq.setQuestion(question);
		questionService.addPaperQuestion(pq);
	}

	
}